package com.mmx.task.node;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.expression.ExpressionUtil;
import com.mmx.constant.NodeConstant;
import com.mmx.domain.bean.FlowData;
import com.mmx.domain.bean.NodeData;
import com.mmx.task.AbstractTaskNode;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc: 包容网关节点
 * @author: liushuai
 * @date: 2023/1/31 15:23
 */
@Service(NodeConstant.INCLUSIVE_GATEWAY)
public class InclusiveGatewayTaskNode extends AbstractTaskNode {
    @Override
    public List<NodeData> execute(FlowData flowObj, NodeData currentNode, Map<String, Object> variables) {
        List<NodeData> targerNode = this.getTargerNode(flowObj, currentNode);
        if(targerNode.size() == 0){
            throw new RuntimeException("The containment gateway does not have a target node");
        }
        List<NodeData> list = new ArrayList<>();
        for(NodeData nodeData : targerNode) {
            if(TRANSIT_NODE.contains(nodeData.getType())){
                throw new RuntimeException("Containment Gateway The destination cannot be a gateway");
            }
            if(StrUtil.isEmpty(nodeData.getCondition())){
                list.add(nodeData);
                continue;
            }
            Boolean condition = (Boolean) ExpressionUtil.eval(nodeData.getCondition(), variables);
            if(condition){
                list.add(nodeData);
            }
        }
        return list;
    }
}
